//  this library.
import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hktk_app/model/industry/category_model.dart';
import 'package:hktk_app/util/font_util.dart';
import 'package:hktk_app/util/view_util.dart';

// _categoryList = IndustryDao.getCategoryModelsSel();

class CateGoryWidget extends StatefulWidget{
  final StreamController<List> controller;
  final List<CategoryModel> categoryList;
  late int selectedIndex;

  // 获取科目列表，不嵌套多级
  CateGoryWidget({super.key,required this.controller,required this.categoryList,this.selectedIndex=0}) ;
  @override
  State<StatefulWidget> createState() => _CateGoryWidgetState();

}

class _CateGoryWidgetState extends State<CateGoryWidget>  with TickerProviderStateMixin{

  late TabController _controller=TabController(
      length: widget.categoryList.length,vsync:this,initialIndex: widget.selectedIndex
  );
  @override
  void initState() {
    //   initState
    super.initState();


  }
  @override
  void dispose() {
    //   dispose
    super.dispose();
    _controller.dispose();
  }
  @override
  Widget build(BuildContext context) {
    // 
    return TabBar(
      controller: _controller,
      // 可滑动
      isScrollable: true,
      // 从左到右进行排列，设置tab左侧对齐，去除左侧偏移
      tabAlignment: TabAlignment.start,
      // 分隔线高度
      dividerHeight: 0,
      // tab间距修改
      labelPadding: EdgeInsets.zero,
      // 选中样式
      // indicator:
      // ChapterTagDecoration(isAtLast: widget.selectedIndex == widget.categoryList.length - 1),
      // 点击监听
      onTap: (index){
        widget.selectedIndex=index;
        widget.controller.add([index,widget.categoryList[index]]);
      },
      tabs: _createTabs(),
    );
  }
  /// 显示的tab
  _createTabs() {
    List<Tab> tabs = [];
    int i = 0;
    for (CategoryModel model in widget.categoryList) {
      tabs.add(Tab(
        child: SizedBox(
          height: 46,
          child: Row(
            children: [
              Text(
                model.name!,
                style: widget.selectedIndex == i
                    ? FontUtil.fontBlue1(30)
                    : FontUtil.fontBlack1(30),
              ),
              hiSpace(width: i != widget.categoryList.length - 1 ? 48 : 10)
            ],
          ),
        ),
      ));
      i++;
    }
    return tabs;
  }
}